package config

import (
	"common/helper"
	"database/sql"
)

/*
三方游戏抽税梯度配置表
*/

type CfgTaxItem struct {
	Id           string  `json:"id" db:"id"`
	ProfitAmount float64 `json:"profit_amount" db:"profit_amount"`
	TaxRate      int     `json:"tax_rate" db:"tax_rate"`
}
type CsTaxCfg struct{}

var cfgTaxList []CfgTaxItem

func (that *CsTaxCfg) GetCfg() []CfgTaxItem {
	return cfgTaxList
}

func (that *CsTaxCfg) removeAllCfg() {
	cfgTaxList = []CfgTaxItem{}
}

func GetTaxRate(profitAmount float64) int {
	var maxRate int
	for _, tax := range cfgTaxList {
		if profitAmount > tax.ProfitAmount {
			maxRate = tax.TaxRate
		} else {
			break // 如果盈利金额小于等于当前阈值，直接跳出循环，返回当前最大税率
		}
	}
	return maxRate
}

func TaxCfgRemove() {
	cfgTaxList = []CfgTaxItem{}
}

func TaxCfgSetRow(rows *sql.Rows) {
	var tVal CfgTaxItem
	var err error
	err = rows.Scan(&tVal.Id, &tVal.ProfitAmount, &tVal.TaxRate)
	if err != nil {
		helper.InfoLog("TaxCfgSetRow,scan err")
		return
	}

	cfgTaxList = append(cfgTaxList, tVal)
}
